package my.learn.tx.onXML.dao;

import my.learn.tx.onXML.api.dao.AccountDao;
import my.learn.tx.onXML.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;


public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    @Override
    public Account findById(Integer accountId) {
        List<Account> accounts = super.getJdbcTemplate().query("select * from account where id=?",
                new BeanPropertyRowMapper<Account>(Account.class), accountId);
        return accounts.isEmpty()?null:accounts.get(0);
    }

    @Override
    public Integer updateAccount(Account account) {

        return super.getJdbcTemplate()
                .update("update account set name= ?, money = ? where id= ?", account.getName(), account.getMoney(), account.getId());
    }
}
